'''
@Author       : 冯浩
@Date         : 2019-12-09 00:02:01
@LastEditors  : 冯浩
@LastEditTime : 2019-12-19 20:58:03
@FilePath     : \bob_develop\python\获取网页cookie.py
'''

from selenium import webdriver
import time
import json
# chromeDriverPath = r'C:\Python\webdriver\chromedriver.exe'


def save_cookies():
    driver = webdriver.Chrome()
    driver.get('https://xueqiu.com/')
    # print(driver.current_url)
    # text = driver.page_source
    cookie = driver.get_cookies()
    # print(cookie)
    jsonCookies = json.dumps(cookie)
    with open(r'.\xueqiu_spider\xueqiucookies.json', 'w') as f:  # 父目录下建文件
        f.write(jsonCookies)


def read_cookies():
    with open(r'.\xueqiu_spider\xueqiucookies.json', 'r') as f:
        cookie_r = json.loads(f.read())
    cookie_list = [i['name'] + '=' + i['value'] for i in cookie_r]
    cookie_str = '; '.join(cookie_list)
    print(cookie_str)


if __name__ == "__main__":
    save_cookies()
    read_cookies()
